package top.lyc829.zzl_android.ui.message;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.MsgNumberBean;
import top.lyc829.zzl_android.logic.bean.NoticeCommentBean;
import top.lyc829.zzl_android.logic.bean.NoticeSystemBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.database.MyDataBase;
import top.lyc829.zzl_android.logic.database.dao.NoticeCommentDao;
import top.lyc829.zzl_android.logic.database.dao.NoticeSystemDao;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class MessageViewModel extends AndroidViewModel {

    private MutableLiveData<MsgNumberBean> msgNumberLiveData;

    private NoticeCommentDao noticeCommentDao;
    private NoticeSystemDao noticeSystemDao;

    public MessageViewModel(@NonNull Application application) {
        super(application);
        MyDataBase myDataBase = MyDataBase.getInstance(application);
        noticeCommentDao = myDataBase.getNoticeCommentDao();
        noticeSystemDao = myDataBase.getNoticeSystemDao();
    }

    public MutableLiveData<MsgNumberBean> getMsgNumberLiveData() {
        if (msgNumberLiveData == null) {
            msgNumberLiveData = new MutableLiveData<>();
        }
        return msgNumberLiveData;
    }

    public void clearMsg() {
//        Network.clearAllMessages(new HttpUtil.MyCallback() {
//            @Override
//            public void success(Response res) throws IOException {
//                String resultStr = res.body().string();
//
//                ResultBean resultBean = new Gson().fromJson(resultStr, ResultBean.class);
//
//                if (resultBean.getCode().equals(200)) {
//                    getMsgNumberLiveData().postValue(new MsgNumberBean());
//                    noticeCommentDao.deleteAllNoticeComments();
//                    noticeSystemDao.deleteAllNoticeSystems();
//                    Utils.showToastSync("清除成功");
//                } else {
//                    failed(new IOException(resultBean.getMsg()));
//                }
//            }
//
//            @Override
//            public void failed(IOException e) {
//                Utils.Sout(e.getMessage());
//            }
//        });

        loadNoticeApply(Utils.getUserId());
    }

    public void loadNoticeApply(int userId) {
        Network.loadNoticeApply(userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<List<NoticeCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resStr, NoticeCommentBean.class);
                if (resultBean.getCode().equals(200)) {
                    //getNoticeApplyLiveData().postValue(resultBean.getData());
                    mergeApplyData(resultBean.getData());
                    loadNoticeFollowUsers(Utils.getUserId());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void loadNoticeFollowUsers(int userId) {
        Network.loadNoticeFollowUsers(userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<List<NoticeCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resStr, NoticeCommentBean.class);
                if (resultBean.getCode().equals(200)) {
                    //getNoticeFollowUserLiveData().postValue(resultBean.getData());
                    mergeFollowData(resultBean.getData());
                    loadNoticePraises(Utils.getUserId());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void loadNoticePraises(int userId) {

        Network.loadNoticePraises(userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<List<NoticeCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resStr, NoticeCommentBean.class);
                if (resultBean.getCode().equals(200)) {
                    mergePraiseData(resultBean.getData());
                    loadNoticeComments(Utils.getUserId());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });

    }

    public void loadNoticeComments(int userId) {
        Network.getNoticeComments(userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<List<NoticeCommentBean>> resultBean = JsonUtilNew.fromJsonArray(resStr, NoticeCommentBean.class);
                if (resultBean.getCode().equals(200)) {
                    mergeReplayData(resultBean.getData());
                    loadNoticeSystems(Utils.getUserId());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    public void loadNoticeSystems(int userId) {
        Network.loadNoticeSystem(userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                String resStr = res.body().string();
                ResultBean<List<NoticeSystemBean>> resultBean = JsonUtilNew.fromJsonArray(resStr, NoticeSystemBean.class);
                if (resultBean.getCode().equals(200)) {

                    mergeSystemData(resultBean.getData());
                    updateMsgNumber();
                    //getNoticeSystemsLiveData().postValue(resultBean.getData());
                    Utils.showToastSync("清除成功");
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }

    private void mergeSystemData(List<NoticeSystemBean> data) {
        if (data != null) {
            for (NoticeSystemBean system : data) {
                if (noticeSystemDao.findItemById(system.getId()) == null) {
                    noticeSystemDao.insertNoticeSystems(system);
                }
            }
        }
    }

    private void mergeApplyData(List<NoticeCommentBean> data) {
        if (data != null) {
            for (NoticeCommentBean system : data) {
                if (noticeCommentDao.findItemById(system.getId()) == null) {
                    system.setNoticeType(NoticeType.APPLY);
                    system.setCurrentLoginUserId(Utils.getUserId());
                    noticeCommentDao.insertNoticeComments(system);
                }
            }
        }
    }


    private void mergeFollowData(List<NoticeCommentBean> data) {
        if (data != null) {
            for (NoticeCommentBean system : data) {
                if (noticeCommentDao.findItemById(system.getId()) == null) {
                    system.setNoticeType(NoticeType.FOLLOW);
                    system.setCurrentLoginUserId(Utils.getUserId());
                    noticeCommentDao.insertNoticeComments(system);
                }
            }
        }
    }

    private void mergePraiseData(List<NoticeCommentBean> data) {
        if (data != null) {
            for (NoticeCommentBean system : data) {
                if (noticeCommentDao.findItemById(system.getId()) == null) {
                    system.setNoticeType(NoticeType.PRAISE);
                    system.setCurrentLoginUserId(Utils.getUserId());
                    noticeCommentDao.insertNoticeComments(system);
                }
            }
        }
    }


    protected void mergeReplayData(List<NoticeCommentBean> data) {
        if (data != null) {
            for (NoticeCommentBean system : data) {
                if (noticeCommentDao.findItemById(system.getId()) == null) {
                    system.setNoticeType(NoticeType.COMMENT);
                    system.setCurrentLoginUserId(Utils.getUserId());
                    noticeCommentDao.insertNoticeComments(system);
                }
            }
        }
    }


    public void updateMsgNumber() {
        Network.getMsgNumber(new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {
                ResultBean<MsgNumberBean> resultBean = JsonUtilNew.fromJsonObject(res.body().string(), MsgNumberBean.class);

                if (resultBean.getCode().equals(200)) {
                    getMsgNumberLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {

            }
        });
    }

}
